﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;



namespace SiteTreeView
{
    /// <summary>
    /// This web part is to be used for displaying sites and sub-sites
    /// that a specific user has access to.  This web part is inheriting
    /// from the SharePoint namespace and uses the SPTreeView and
    /// SPHierarchyDataSourceControl to create a Security Trimmed view
    /// for the user.
    /// </summary>
    
    public class TreeNavigation : System.Web.UI.WebControls.WebParts.WebPart
    {
        // Create the SPTreeView and SPHierarchyDataSourceControl so that
        // they can be seen by the CreateChildControls and Render methods
        private SPTreeView myTreeView;
        private SPHierarchyDataSourceControl myDataSource;

        /// <summary>
        /// Create the references for the TreeView and DataSource
        /// in the CreateChildControls method and add the controls
        /// </summary>
        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            // Setup myDataSource to show only the Child webs
            myDataSource = new SPHierarchyDataSourceControl();
            myDataSource.ID = "myTreeViewDataSource";
            myDataSource.RootContextObject = "Web";
            myDataSource.IncludeDiscussionFolders = false;
            myDataSource.ShowDocLibChildren = false;
            myDataSource.ShowFolderChildren = false;
            myDataSource.ShowListChildren = false;
            myDataSource.ShowWebChildren = true;
            Controls.Add(myDataSource);

            // Setup myTreeView to look like the quickLaunch TreeView
            myTreeView = new SPTreeView();
            myTreeView.ID = "myTreeView";
            myTreeView.DataSourceID = "myTreeViewDataSource";
            myTreeView.EnableClientScript = true;
            myTreeView.EnableViewState = true;
            myTreeView.NodeIndent = 12;
            myTreeView.NodeStyle.CssClass = "ms-navitem";
            myTreeView.NodeStyle.HorizontalPadding = 2;
            myTreeView.SelectedNodeStyle.CssClass = "ms-tvselected";
            myTreeView.ExpandDepth = 1; // Change this number for more sub-sites
            myTreeView.ExpandImageUrl = "/_layouts/images/tvplus.gif";
            myTreeView.CollapseImageUrl = "/_layouts/images/tvminus.gif";
            myTreeView.NoExpandImageUrl = "/_layouts/images/tvblank.gif";
            myTreeView.SkipLinkText = "";
            myTreeView.ShowLines = true;
            Controls.Add(myTreeView);
        }

        /// <summary>
        /// Create the view of the web part.  Adding a DIV that grabs
        /// the ms-treeviewouter class, then renders the DataSource and
        /// TreeView to the web part.
        /// </summary>
        /// <param name="writer"></param>
        protected override void Render(HtmlTextWriter writer)
        {
            try
            {
                EnsureChildControls();
                writer.Write("<div class='ms-treeviewouter'>");
                myDataSource.RenderControl(writer);
                myTreeView.RenderControl(writer);
                writer.Write("</div>");
            }
            catch (Exception ex)
            {
                writer.Write(string.Format("Message: {0}<br>Stack: {1}<br>Source: {2}<br>",
                    ex.Message, ex.StackTrace, ex.Source));
            }
        }
    }
}
